package com.mmall.common;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.io.Serializable;

/**
 * Created by Administrator on 2017/12/6.
 */
@JsonSerialize( include = JsonSerialize.Inclusion.NON_NULL)
//保证序列化json对象的时候如果是null的对象 key也会消失
public class ServerResponse <T> implements Serializable {

    private  int status;
    private  String  msg;
    private  T data;
    //私有构造器

    private ServerResponse(int status ){
        this.status=status;
    }

    private ServerResponse(int status,String msg ){
        this.status=status;
        this.msg=msg;
    }

    private ServerResponse(int status ,T data){
        this.status=status;
        this.data=data;
    }

    private ServerResponse(int status,String msg, T data ){
        this.status=status;
        this.msg=msg;
        this.data=data;
    }

    /*
    * 测试一下当参数 int status  String msg 是调用的方法是什么
    * 是private ServerResponse(int status ,T data)还是
    * rivate ServerResponse(int status,String msg )
    * */
    public static void main(String[] args) {
        ServerResponse s1=new ServerResponse(1,new Object());
        ServerResponse s2=new ServerResponse(1,"sdd");
        /*sout tab 两下*/
        System.out.println("console");
    }

    @JsonIgnore
    //使之不在序列化结果中显示
    public  boolean isSuccess(){ //采用枚举形式
        return  this.status==ResponseCode.SUCCESS.getCode();  //如果状态是0  返回true   如果不是0返回false
    }

    public  int getStatus(){
        return  status;
    }

    public  String  getMsg(){
        return  msg;
    }
    public  T getData(){
        return  data;
    }


    //创建静态方法
    public  static  <T> ServerResponse<T> createBySuccess(){
        return  new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
    }

    public  static <T> ServerResponse<T> createBySuccessMessage(String msg){
        return  new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
    }

    public  static  <T> ServerResponse<T> createBySuccess(T data){
        return  new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),data);
    }

    public  static  <T> ServerResponse<T> createBySuccess(String msg,T data){
        return  new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg,data);
    }

    public  static  <T> ServerResponse<T> createByError(){
        return  new ServerResponse<T>(ResponseCode.ERROR.getCode());
    }

    public  static  <T> ServerResponse<T> createByErrorMessage(String Errormessage){
        return  new ServerResponse<T>(ResponseCode.ERROR.getCode(),Errormessage);
    }

    public  static  <T> ServerResponse<T> createByErrorCodeMessage(int errorCode ,String errorMessage){
        return  new ServerResponse<T>(errorCode, errorMessage);
    }



}
